package com.szdvte.news.action.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.szdvte.news.bean.News;
import com.szdvte.news.bean.NewsType;
import com.szdvte.news.service.INewsService;
import com.szdvte.news.service.INewsTypeService;


public class NewsAction extends BaseAction{
	private News news;
	private List<News> list = new ArrayList<News>();
	private INewsService newsService;
	
	
	//private NewsType newsType;
	private List<NewsType> newsTypeList = new ArrayList<NewsType>();		
	private INewsTypeService newsTypeService;
	 
	
	
	
	private String saveDir = "images";
	private File file;// 临时文件
	private String fileFileName;// 文件名字

	private String id;// 文件ID
	private InputStream is;// 用于文件下载的一个输入流
	
	
	
	//默认
	public String execute(){
		newsTypeList = newsTypeService.select();
		return "add";
	}
	
	
	//列表
	public String select(){
		show();
		return "news";
	}
	
	private void show(){
		list = newsService.select(news);
		
		
		ServletActionContext.getRequest().getSession().setAttribute("newsList", list);
		
	}
	//添加
	public String add(){
		setFlag(1);
		
		
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.out.println(sdf.format(date));
		
		String dateStr = sdf.format(date);
		
		Date d = new Date();
		try {
			d = sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		news.setNewsData(d);
		//news.setNewsData(new Date());
		newsTypeList = newsTypeService.select();
		if(newsService.add(news,this.upload(null))){
			setFlag(2);
		}else{
			setFlag(3);
		}
		return "add";
	}
	//修改
	public String update(){
		setFlag(1);
		news.setNewsData(new Date());
		if(newsService.update(news)){
			setFlag(2);
		}else{
			setFlag(3);
		}
		System.out.println(news);
		news = newsService.load(news);
		list = newsService.loadType(news);
		return "load";
	}
	public String load(){
		news = newsService.load(news);
		list = newsService.loadType(news);
		
		
		
		return "load";
	}
	//删除
	public String delete(){
		setFlag(1);
		if(newsService.delete(news)){
			setFlag(2);
		}else{
			setFlag(3);
		}
		show();
		return "news";
	}
	
	/**
	 * 把图片写到服务器中去
	 * @return
	 */
	public String upload(String url){
		String path="";
		if(url!=null && url.length()>0){
			path=url;
		}
		//判断文件上传的文件是否为空
		if(file!=null){
			String id = UUID.randomUUID().toString();
			id = id.replace("-", "");
			String fileType = this.fileFileName.split("\\.")[1];
			if(path==""){
				path = this.saveDir + "/" + id+"."+fileType;
			}
			ServletContext application = ServletActionContext.getServletContext();
			String realPath = application.getRealPath(path);
			InputStream is = null;
			OutputStream os = null;
			byte[] b = new byte[1024];// 缓存

			try {
				is = new FileInputStream(this.file);
				os = new FileOutputStream(realPath);
				int len = is.read(b);
				while (len != -1) {
					os.write(b, 0, len);
					len = is.read(b);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(is!=null){
					try {
						is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(os!=null){
					try {
						os.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		

		return path;
	}
	
	public News getNews() {
		return news;
	}
	public void setNews(News news) {
		this.news = news;
	}
	public INewsService getNewsService() {
		return newsService;
	}
	public void setNewsService(INewsService newsService) {
		this.newsService = newsService;
	}
	
	public INewsTypeService getNewsTypeService() {
		return newsTypeService;
	}
	public void setNewsTypeService(INewsTypeService newsTypeService) {
		this.newsTypeService = newsTypeService;
	}	
	
	
	public List<News> getList() {
		return list;
	}
	public void setList(List<News> list) {
		this.list = list;
	}
	public File getFile() {
		return file;
	}
	public void setFile(File file) {
		this.file = file;
	}
	public String getFileFileName() {
		return fileFileName;
	}
	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public InputStream getIs() {
		return is;
	}
	public void setIs(InputStream is) {
		this.is = is;
	}
	
	public List<NewsType> getNewsTypeList() {
		return newsTypeList;
	}
	public void setNewsTypeList(List<NewsType> newsTypeList) {
		this.newsTypeList = newsTypeList;
	}
	
}
